package com.app.mingluxing.base;

import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.widget.Toast;

import com.app.mingluxing.utils.CommonUtils;
import com.app.mingluxing.utils.Constant;

/**
 * Created by Administrator on 2015/7/20.
 */
public class BaseFragment extends Fragment {

    protected MyApplication mApplication;
    protected SharedPreferences sp;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mApplication = (MyApplication) getActivity().getApplication();
        sp = getActivity().getSharedPreferences(Constant.CONFIG, Context.MODE_PRIVATE);
    }
    /**
     * 土司
     * @param text
     */
    public void showToast(final String text) {
        if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
            // 如果在主线程中
            showToash(text);

        } else {
            // 在子线程中
            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    showToash(text);
                }
            });

        }
    }

    private void showToash(final String text) {

        try {
            if (!CommonUtils.isNull(text)) {
                Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}
